package com.investment.advisor.service.impl;

import com.investment.advisor.dto.FactorDTO;
import com.investment.advisor.entity.Factor;
import com.investment.advisor.repository.FactorRepository;
import com.investment.advisor.service.FactorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FactorServiceImpl implements FactorService {

    private final FactorRepository factorRepository;

    @Autowired
    public FactorServiceImpl(FactorRepository factorRepository) {
        this.factorRepository = factorRepository;
    }

    @Override
    public List<FactorDTO> getAllFactors() {
        return factorRepository.findAll().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public FactorDTO getFactorById(Long id) {
        return factorRepository.findById(id)
                .map(this::convertToDTO)
                .orElseThrow(() -> new EntityNotFoundException("Factor not found with id: " + id));
    }

    @Override
    public List<FactorDTO> getFactorsByCategory(String category) {
        return factorRepository.findByCategory(category).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<FactorDTO> searchFactors(String keyword) {
        return factorRepository.findByNameContaining(keyword).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public FactorDTO createFactor(FactorDTO factorDTO) {
        Factor factor = convertToEntity(factorDTO);
        Factor savedFactor = factorRepository.save(factor);
        return convertToDTO(savedFactor);
    }

    @Override
    @Transactional
    public FactorDTO updateFactor(Long id, FactorDTO factorDTO) {
        if (!factorRepository.existsById(id)) {
            throw new EntityNotFoundException("Factor not found with id: " + id);
        }
        
        Factor factor = convertToEntity(factorDTO);
        factor.setId(id);
        Factor updatedFactor = factorRepository.save(factor);
        return convertToDTO(updatedFactor);
    }

    @Override
    @Transactional
    public void deleteFactor(Long id) {
        if (!factorRepository.existsById(id)) {
            throw new EntityNotFoundException("Factor not found with id: " + id);
        }
        factorRepository.deleteById(id);
    }
    
    private FactorDTO convertToDTO(Factor factor) {
        FactorDTO factorDTO = new FactorDTO();
        BeanUtils.copyProperties(factor, factorDTO);
        return factorDTO;
    }
    
    private Factor convertToEntity(FactorDTO factorDTO) {
        Factor factor = new Factor();
        BeanUtils.copyProperties(factorDTO, factor, "id", "createTime", "updateTime", "strategies");
        return factor;
    }
} 